package com.htj.kf2b.construct_mobile.controller;


import cn.hutool.core.codec.Base64;
import cn.hutool.core.text.StrSplitter;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.htj.cross.util.Des;
import com.htj.cross.webservice.CrossService;
import com.htj.cross.webservice.CrossService_Service;

/**
 * import com.sun.tools.javac.code.Attribute;
 */

import org.apache.commons.collections.map.LinkedMap;
import org.springframework.web.bind.annotation.*;
import org.tempuri.ArrayOfString;

import java.lang.reflect.Array;
import java.util.*;


@RestController
public class SignController {


    /**
     * 根据phone planNumber inOrOut Position 插入签到信息
     *
     * @param map
     * @return
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @PostMapping(value = "/sign")
    @ResponseBody
    public JSONObject setSign(@RequestParam Map<String, Object> map) {

        String phone = (String) map.get("phone");
        String planNumber = (String) map.get("planNumber");
        String inOrOut = (String) map.get("inOrOut");
        String position = (String) map.get("position");

        //插入数据库逻辑

        CrossService_Service cs = new CrossService_Service();
        CrossService service = cs.getCrossServiceImplPort();
        Des ds = new Des();/*加密文件*/
        String str = ds.strEnc("construct.CallSpring.setSign", "A08FAEF81E115E2D075", "", "");/*第一个参数是程序集.文件名.方法名 第二个参数是内网配置的秘钥*/
        ArrayOfString data = new ArrayOfString();
        data.getString().add(phone);/*内外方法的参数*/
        data.getString().add(planNumber);/*内外方法的参数*/
        data.getString().add(inOrOut);
        data.getString().add(position);
        data.getString().add("");/*预留参数*/
        data.getString().add("");/*预留参数*/
        String r = service.jGetJson(str, "construct", data);
        System.out.println(r);
        JSONObject jsonObject = JSONUtil.createObj();
        if (r.equals("success") ) {
            jsonObject.putOnce("code", "200");
            jsonObject.putOnce("msg", "成功");
        } else {
            jsonObject.putOnce("code", "404");
            jsonObject.putOnce("msg", "失败");
        }


        return jsonObject;
    }


    /**
     * 根据电话号获取用户的签到信息
     * 获取的信息为startMark + 10
     *
     * @param phone
     * @return
     */
    @CrossOrigin
    @RequestMapping(value = "/sign", method = RequestMethod.GET)
    public JSONObject getSignByPhone(String phone, String startMark) {

        //TODO 根据phone 获取用户信息

//        //模拟返回值
//        ArrayList arrayList = new ArrayList();
//        LinkedMap map = new LinkedMap();
//        map.put("planNumber","1234");
//        map.put("state","签到");
//        map.put("date","2022-02-02-09:00");
//        LinkedMap map1 = new LinkedMap();
//        map1.put("planNumber","12345");
//        map1.put("state","签退");
//        map1.put("date","2022-02-02-11:00");
//
//       arrayList.add(map);
//       arrayList.add(map1);


        CrossService_Service cs = new CrossService_Service();
        CrossService service = cs.getCrossServiceImplPort();
        Des ds = new Des();/*加密文件*/
        String str = ds.strEnc("construct.CallSpring.getUserSignListByPhone", "A08FAEF81E115E2D075", "", "");/*第一个参数是程序集.文件名.方法名 第二个参数是内网配置的秘钥*/
        ArrayOfString data = new ArrayOfString();
        data.getString().add(phone);/*内外方法的参数*/
        data.getString().add(startMark);/*内外方法的参数*/
        data.getString().add("");/*预留参数*/
        data.getString().add("");/*预留参数*/
        String r = service.jGetJson(str, "construct", data);
        String resultBase64 = Base64.decodeStr(r);


        String resultSting = resultBase64.substring(0, resultBase64.length() - 1);

        System.out.println(resultSting);

        ArrayList arrayList = new ArrayList();


        List<String> split = StrSplitter.split(resultSting, '}', 0, true, true);
        System.out.println(split);


        for (String s : split) {
            System.out.println(s.substring(1)+"}");
            arrayList.add(JSONUtil.parseObj(s.substring(1)+"}"));
        }


        JSONObject jsonObject = JSONUtil.createObj();
        jsonObject.putOnce("code", "200");
        jsonObject.putOnce("msg", "成功");
        jsonObject.putOnce("phone", phone);
        jsonObject.putOnce("sign", arrayList);
        jsonObject.putOnce("starMark", startMark);
//        jsonObject.putOnce("length",jsonObject1.size());
        return jsonObject;
    }

    /**
     * 根据电话号获取签到的总数量
     * @param phone
     * @return
     */
    @CrossOrigin
    @RequestMapping(value = "/length")
    public JSONObject getCode(String phone)
    {

         CrossService_Service cs = new CrossService_Service();
        CrossService service = cs.getCrossServiceImplPort();
        Des ds = new Des();/*加密文件*/
        String str = ds.strEnc("construct.CallSpring.getSignListlength","A08FAEF81E115E2D075", "", "");/*第一个参数是程序集.文件名.方法名 第二个参数是内网配置的秘钥*/
        ArrayOfString data = new ArrayOfString();
        data.getString().add(phone);/*内外方法的参数*/
        data.getString ().add("");/*预留参数*/
        data.getString().add("");/*预留参数*/
        String r = service.jGetJson(str, "construct", data);


         JSONObject jsonObject = JSONUtil.createObj();

        jsonObject.putOnce("code", "200");
        jsonObject.putOnce("msg", "成功");
        jsonObject.putOnce("phone", phone);
        jsonObject.putOnce("number", r);



        return jsonObject;
    }
}


